#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 5e4 + 5;

int n, m;
int np[N];
int primes[N], tot;
void init(int n) {
  np[1] = 1;
  rep(i, 2, n) {
    if (!np[i]) primes[++tot] = i;
    rep(j, 1, tot) {
      ll k = 1ll * primes[j] * i;
      if (k > n) break;
      np[k] = 1;
      if (i % primes[j] == 0) break;
    }
  }
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
bool check(int x, int a0, int a1, int b0, int b1) {
  int d = gcd(x, a0);
  if (d != a1) return false;
  d = gcd(x, b0);
  ll y = 1ll * x / d * b0;
  return y == b1;
}
const int M = 55;
pii factors[M];
int fcnt;
int dividers[N], dcnt;
int ans;
void dfs(int i, int p) {
  if (i > fcnt) {
    dividers[++dcnt] = p;
    return;
  }
  int b = factors[i].first, k = factors[i].second;
  int x = 1;
  dfs(i + 1, p);
  rep(j, 1, k) {
    x *= b;
    dfs(i + 1, p * x);
  }
}
void solve() {
  int a0, a1, b0, b1;
  cin >> a0 >> a1 >> b0 >> b1;
  ans = fcnt = dcnt = 0;
  int x = b1;
  rep(i, 1, tot) {
    if (x % primes[i] == 0) {
      int p = 0;
      while (x % primes[i] == 0) p++, x /= primes[i];
      factors[++fcnt] = pii(primes[i], p);
    }
  }
  if (x != 1) factors[++fcnt] = pii(x, 1);
  dfs(1, 1);
  rep(i, 1, dcnt) {
    x = dividers[i];
    if (check(x, a0, a1, b0, b1)) ans++;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init(sqrt(2e9 + 5));
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}